/**
 * 封装缓存
 */
class Cache {
	// 写入缓存
	set(name, data) {
		let result = false
		try {
			uni.setStorageSync(name, data)
			result = true
		} catch (e) {
			console.log(`设置缓存失败：${e}`)
		}
		return result
	}

	// 获取缓存
	get(name) {
		let result = null
		try {
			const value = uni.getStorageSync(name)
			if (value) result = value
		} catch (e) {
			console.log(`获取缓存失败：${e}`)
		}
		return result
	}

	// 删除缓存
	delete(name) {
		let result = false
		try {
			uni.removeStorageSync(name)
			result = true
		} catch (e) {
			console.log(`删除缓存失败：${e}`)
		}
		return result
	}

	// 判断缓存是否存在
	has(name) {
		let result = false
		try {
			const value = uni.getStorageSync(name)
			if (value) result = true
		} catch (e) {
			console.log(`检查缓存是否存在失败：${e}`)
		}
		return result
	}

	// 清除缓存
	clear() {
		let result = false
		try {
			uni.clearStorageSync()
			result = true
		} catch (e) {
			console.log(`清除缓存失败：${e}`)
		}
		return result
	}

	// 清除一些登录缓存
	clearCache() {
		uni.removeStorageSync('id')
		uni.removeStorageSync('token')
		uni.removeStorageSync('phone')
		uni.removeStorageSync('type')
		uni.removeStorageSync('name')
		uni.removeStorageSync('image')
	}
}

export default Cache
